package com.pandy.transaction;

import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

/**
 * @description:
 * @author: panhong
 * @date: 2025/3/19
 */

@Component
public class UserService {

    private String name;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Transactional
    public void methodA() {
        jdbcTemplate.update("insert into user values(null,'zhangsan',18)");

        int i = 1 / 0;
    }

    // 自己注入自己
    @Autowired
    private UserService userService;
    public void methodB() {
        //this.methodA(); // 方法内的自调用
        userService.methodA();
    }


    public void methodC() {
        //this.methodA(); // 方法内的自调用
        //@EnableAspectJAutoProxy(exposeProxy = true)
        UserService userService = (UserService)AopContext.currentProxy();
        userService.methodA();
    }
}
